import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:provider/provider.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:wan_android_flutter/common_ui/smart_refresh/smart_refresh_widget.dart';
import '../../../common_ui/common_style.dart';
import '../../../common_ui/web/webview_page.dart';
import '../../../common_ui/web/webview_widget.dart';
import '../../../repository/datas/knowledge_detail_list_data.dart';
import '../../../route/RouteUtils.dart';
import 'knowledge_detail_vm.dart';

class knowledgeTabChildPage extends StatefulWidget {
  const knowledgeTabChildPage({super.key, this.cid});

  final String? cid;

  @override
  State<StatefulWidget> createState() {
    return _knowledgeTabChildPageState();
  }
}

class _knowledgeTabChildPageState extends State<knowledgeTabChildPage> {
  knowledgeDetailViewModel viewModel = knowledgeDetailViewModel();
  RefreshController refreshController = RefreshController();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    refreshOrLoad(false);
  }

  void refreshOrLoad(bool loadMore) {
    viewModel.getDetailList(widget.cid, loadMore).then((value) {
      if (loadMore) {
        refreshController.loadComplete();
      } else {
        refreshController.refreshCompleted();
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (context) {
        return viewModel;
      },
      child: Container(
        child: Scaffold(
          body: Consumer<knowledgeDetailViewModel>(
            builder: (context, vm, child) {
              return SmartRefreshWidget(
                controller: refreshController,
                onRefresh: () {
                  refreshOrLoad(false);
                },
                onLoading: () {
                  refreshOrLoad(true);
                },
                child: ListView.builder(
                  itemBuilder: (context, index) {
                    var item = vm.detailList[index];
                    return _item(item, onTap: () {
                      //跳转到网页
                      RouteUtils.push(
                          context,
                          WebViewPage(
                            loadResource: item.link ?? "",
                            webViewType: WebViewType.URL,
                            showTitle: true,
                            title: item.title,
                          ));
                    });
                  },
                  itemCount: vm.detailList.length,
                ),
              );
            },
          ),
        ),
      ),
    );
  }

  Widget _item(KnowledgeDetailItemData item, {GestureTapCallback? onTap}) {
    return GestureDetector(
        onTap: onTap,
        child: Container(
            margin: EdgeInsets.symmetric(vertical: 8.h, horizontal: 15.w),
            padding: EdgeInsets.all(10.r),
            decoration: BoxDecoration(
                border: Border.all(color: Colors.black12, width: 0.5.r)),
            child: Column(children: [
              Row(
                children: [
                  normalText(item.superChapterName),
                  const Expanded(child: SizedBox()),
                  Text("${item.niceShareDate}"),
                ],
              ),
              Text("${item.title}", style: titleTextStyle15),
              Row(
                children: [
                  normalText(item.chapterName),
                  const Expanded(child: SizedBox()),
                  Text("${item.shareUser}"),
                ],
              )
            ])));
  }
}
